//
//  HLCycleScrollCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/18.
//

#import "HLCycleScrollCell.h"

@interface HLCycleScrollCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *playIconView;
@end

@implementation HLCycleScrollCell


-(instancetype)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.imageView = [[UIImageView alloc] init];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

    
    self.playIconView = [[UIImageView alloc] init];
    _playIconView.image = UIImageMake(@"video_play_icon");
    [self.contentView addSubview:_playIconView];
    [_playIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
}

- (void)setModel:(HLOpusModel *)model
{
    _model = model;
    _playIconView.hidden = YES;
    _imageView.userInteractionEnabled = NO;
    NSString *imageUrl = model.opusLink;
    if ([model.opusType isEqualToString:@"2"]){
        _imageView.userInteractionEnabled = YES;
        imageUrl = model.videoCoverPic;
        _playIconView.hidden = NO;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateChange) name:HLUserDetailScrollDidChangeNotification object:nil];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playVideo)];
        [_imageView addGestureRecognizer:tapGesture];
    }
    UIImage *place = [UIImage imageNamed:@"avatar_famale_big_placeholder"];
    if (model.isMe) {
       place = [UIImage imageNamed:@"avatar_male_big_placeholder"];
    }

    [_imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:place];
}

- (void)playVideo
{
    _model.isPlay = YES;
    _playIconView.hidden = YES;
}

- (void)playStateChange
{
    if (_model.isPlay){
        [self stopVideo];
    }
}

- (void)stopVideo
{
    _model.isPlay = NO;
    _playIconView.hidden = NO;
    
}

@end
